package org.jeecg.modules.basedata.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.entity.basedata.RentContract;
import org.jeecg.modules.basedata.mapper.RentContractMapper;
import org.jeecg.modules.basedata.service.IRentContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description: 租赁合同
 * @Author: jeecg-boot
 * @Date:   2021-06-29
 * @Version: V1.0
 */
@Service
public class RentContractServiceImpl extends ServiceImpl<RentContractMapper, RentContract> implements IRentContractService {

    @Autowired
    private RentContractMapper rentContractMapper;

    @Override
    public List<RentContract> findPage(Page<RentContract> page, RentContract rentContract){
        return rentContractMapper.findPage(page, rentContract);
    }

    @Override
    public Page<RentContract> queryContractFeeList(Page<RentContract> pageList, RentContract rentContract){
        return rentContractMapper.queryContractFeeList(pageList, rentContract);
    }

    @Override
    public List<RentContract> getAllContractInfo( String projectId){
        return rentContractMapper.getAllContractInfo(projectId);
    }

    @Override
    public  List<RentContract> getContractInfo(List<String> contractIds, String feeId){
        return rentContractMapper.getContractInfo(contractIds, feeId);
    }

    @Override
    public Page<RentContract> getContractList(Page<RentContract> pageList, String projectId,String feeRuleId,String roomId,String floorId){
        return rentContractMapper.getContractList(pageList, projectId, feeRuleId, roomId,floorId);
    }

    @Override
    public List<RentContract> selectByRentContract(RentContract rentContract){
        return rentContractMapper.selectByRentContract(rentContract);
    }

    @Override
    public Result<RentContract> updateRentContract(RentContract rentContract){
        Result<RentContract> result = new Result<RentContract>();
        try {
            RentContract rentContractEntity = this.getById(rentContract.getId());
            if(rentContractEntity==null) {
               return result.error500("未找到对应实体");
            }
            this.updateById(rentContract);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
        return result.ok();
    }

    @Override
    public Result<RentContract> delRentContract(String id){
        Result<RentContract> result = new Result<RentContract>();
        try {
            this.removeById(id);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
        return result.ok();
    }

    @Override
    public Result<RentContract> saveRentContract(RentContract rentContract){
        Result<RentContract> result = new Result<RentContract>();
        try {
            if (rentContract.getEndDate().before(rentContract.getStartDate())){
                return result.error500("合同结束时间不能小于开始时间");
            }
            List<RentContract> rentContractList = rentContractMapper.selectByRentContract(rentContract);
            if (rentContractList.size() > 0){
                return result.error500("合同编号在系统中已存在");
            }
            //判断当前房间是否有合同，并且未过期
            RentContract normal = rentContractMapper.getNormalRoom(rentContract.getRoomId(), rentContract.getFloorId());
            if (normal != null){
                return result.error500("当前房间已经租赁请勿重复添加，合同编号："+ normal.getCode());
            }
            this.save(rentContract);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return result.error500("操作失败");
        }
        return result.ok();
    }

    @Override
    public List<RentContract> selectByMap(Map<String, Object> columnMap) {
        return rentContractMapper.selectByMap(columnMap);
    }
}
